package com.xinghou.platform.dao.main.aspect;

import com.xinghou.platform.dao.main.common.DBUserLevelCached;
import com.xinghou.platform.model.main.UserStatistics;
import com.xinghou.platform.util.JsonUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class UserStaticCacheAspect extends DBUserLevelCached {
	@Pointcut("execution(public * com.xinghou.platform.dao.main.UserStatisticsDAO.getUserStatById(..))")
	public void getUserStatById(){
	}

	@Pointcut("execution(public * com.xinghou.platform.dao.main.UserStatisticsDAO.updateUserStat(..))")
	public void updateUserStat(){
	}

	@Around("getUserStatById()")
	public UserStatistics getUserStatById(ProceedingJoinPoint pjp) throws Throwable{
		String id = ((Object[])pjp.getArgs())[0].toString();
		String key = String.format(CACHE_USERSTATE_STATID , id);
		Object jsonToken = getRedisClient().get(key, CACHED_DB_INDEX);
		if(jsonToken == null){
			UserStatistics userStatistics = (UserStatistics)pjp.proceed();
			if(userStatistics != null)
				getRedisClient().set(key, JsonUtil.toJson(userStatistics), CACHED_DB_INDEX);
			return userStatistics;
		}else{
			UserStatistics userStatistics = (UserStatistics)JsonUtil.fromJson(jsonToken.toString(), UserStatistics.class);
			return userStatistics;
		}
	}
	@Around("updateUserStat()")
	public int updateUserStat(ProceedingJoinPoint pjp) throws Throwable{

		UserStatistics userStatistics = (UserStatistics)((Object[])pjp.getArgs())[0];

		int returnno = (int) pjp.proceed();

		String keyInfoId = String.format(CACHE_USERSTATE_STATID , userStatistics.getStatid());

		if(getRedisClient().exists(keyInfoId, CACHED_DB_INDEX)){
			Object jsonToken = getRedisClient().get(keyInfoId,CACHED_DB_INDEX);
			if(jsonToken !=null) {
				UserStatistics userStatisticsbak = (UserStatistics) JsonUtil.fromJson(jsonToken.toString(), UserStatistics.class);

				if(userStatistics.getAttentioncount()!=null&&userStatistics.getAttentioncount()!=0){
					userStatisticsbak.setAttentioncount(userStatisticsbak.getAttentioncount()+userStatistics.getAttentioncount());
				}

				if(userStatistics.getBuycount()!=null&&userStatistics.getBuycount()!=0){
					userStatisticsbak.setBuycount(userStatisticsbak.getBuycount() + userStatistics.getBuycount());
				}

				if(userStatistics.getDynamiccount()!=null&&userStatistics.getDynamiccount()!=0){
					userStatisticsbak.setDynamiccount(userStatisticsbak.getDynamiccount() + userStatistics.getDynamiccount());
				}

				if(userStatistics.getExposurecount()!=null&&userStatistics.getExposurecount()!=0){
					userStatisticsbak.setExposurecount(userStatisticsbak.getExposurecount() + userStatistics.getExposurecount());
				}

				if(userStatistics.getFanscount()!=null&&userStatistics.getFanscount()!=0){
					userStatisticsbak.setFanscount(userStatisticsbak.getFanscount() + userStatistics.getFanscount());
				}

				if(userStatistics.getLogincount()!=null&&userStatistics.getLogincount()!=0){
					userStatisticsbak.setLogincount(userStatisticsbak.getLogincount() + userStatistics.getLogincount());
				}

				if(userStatistics.getPraisecount()!=null&&userStatistics.getPraisecount()!=0){
					userStatisticsbak.setPraisecount(userStatisticsbak.getPraisecount() + userStatistics.getPraisecount());
				}

				if(userStatistics.getProductcount()!=null&&userStatistics.getProductcount()!=0){
					userStatisticsbak.setProductcount(userStatisticsbak.getProductcount() + userStatistics.getProductcount());
				}

				if(userStatistics.getProjectcount()!=null&&userStatistics.getProjectcount()!=0){
					userStatisticsbak.setProjectcount(userStatisticsbak.getProjectcount() + userStatistics.getProjectcount());
				}

				if(userStatistics.getSalecount()!=null&&userStatistics.getSalecount()!=0){
					userStatisticsbak.setSalecount(userStatisticsbak.getSalecount() + userStatistics.getSalecount());
				}

				if(userStatistics.getSharecount()!=null&&userStatistics.getSharecount()!=0){
					userStatisticsbak.setSharecount(userStatisticsbak.getSharecount() + userStatistics.getSharecount());
				}
				getRedisClient().set(keyInfoId, JsonUtil.toJson(userStatisticsbak), CACHED_DB_INDEX);
			}

		}
		
		return returnno;
	}
	
}
